package com.baishi.smartrefresh;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private QuickAdapter quickAdapter;
    private SmartRefreshLayout refreshLayout;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findID();
        initView();
        initData();
    }

    private void findID() {
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
    }

    private void initView() {
        quickAdapter = new QuickAdapter();
        DividerItemDecoration decor = new DividerItemDecoration(this, VERTICAL);
        decor.setDrawable(ContextCompat.getDrawable(this,R.drawable.line));
        recyclerView.addItemDecoration(decor);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(quickAdapter);
        refreshLayout.setEnableNestedScroll(true);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        quickAdapter.replaceData(list);
                        refreshLayout.finishRefresh();
                    }
                }, 2000);
            }
        });
        quickAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                quickAdapter.addData(list);
                quickAdapter.loadMoreComplete();
            }
        },recyclerView);
    }

    private void initData() {
        quickAdapter.addData("hahahha");
        list = new ArrayList<>();
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        list.add("哈哈哈");
        quickAdapter.addData(list);
    }
}
